<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>投票分组</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  {include file="public/css"/}
</head>
<body>
  <div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-card layui-col-md12">
          <div class="layui-card-header">投票分组</div>
          <div class="layui-card-body" pad15>
						<div class="layui-col-md4" style="padding-bottom:10px">
							<a class="layui-btn layuiadmin-btn-list" href="javascript:void(0)" onclick="edit(0)">添加分组</a>
							<button class="layui-btn layui-btn-primary layuiadmin-btn-list" onclick="datadel(0)">删除</button>
						</div>
						<div class="layui-form layui-col-md8 layui-form-search">
							<div class="layui-inline layuiadmin-input-useradmin">
								<label class="layui-form-label">分组名称</label>
								<div class="layui-input-inline">
									<input type="text" name="name" autocomplete="off" class="layui-input">
								</div>
							</div>
							<div class="layui-inline">
								<button class="layui-btn layuiadmin-btn-replys" lay-submit="" lay-filter="LAY-app-forumreply-search">
									<i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
								</button>
							</div>
						</div>
						<div class="layui-col-md12">
							<table id="tabledata" lay-filter="tabledata"></table>
						</div>
          </div>
        </div>
    </div>
  </div>
	{include file="public/js"/}
	<script>
  var table = layui.table;
	var datawhere = {};
  //数据表
  var tableIns = table.render({
    elem: '#tabledata'
    ,url: "{$Request.url}" //数据接口
    ,page: true //开启分页
    ,cols: [[ //表头
			{type:"checkbox"},
      {field: 'id', title: 'ID',  sort: true,width:80},
      {field: 'name', title: '分组名称',width:200},
      {field: 'sort', title: '排序',width:80},
      {field: 'createtime', title: '创建时间',templet:function(d){ return date('Y-m-d H:i:s',d.createtime)},width:250},

	{field: 'operation', title: '操作',templet:function(d){
		var html = '';
		html += '<button class="table-btn" onclick="edit('+d.id+')">编辑</button>';
		html += '<button class="table-btn" onclick="datadel('+d.id+')">删除</button>';
		return html;
	}}
    ]]
  });
	//排序
	table.on('sort(tabledata)', function(obj){
		datawhere.field = obj.field;
		datawhere.order = obj.type;
		tableIns.reload({
			initSort: obj,
			where: datawhere
		});
	});
	//检索
	layui.form.on('submit(LAY-app-forumreply-search)', function(obj){
		var field = obj.field
		var olddatawhere = datawhere
		datawhere = field
		datawhere.field = olddatawhere.field
		datawhere.order = olddatawhere.order
		tableIns.reload({
			where: datawhere,
			page: {curr: 1}
		});
	})
	
	//删除
	function datadel(id){
		var ids = [];
		if(id==0){
			var checkStatus = table.checkStatus('tabledata')
			var checkData = checkStatus.data; //得到选中的数据
			if(checkData.length === 0){
				 return layer.msg('请选择数据');
			}
			for(var i=0;i<checkData.length;i++){
				ids.push(checkData[i]['id']);
			}
		}else{
			ids.push(id);
		}
		layer.confirm('确定要删除吗？删除后无法恢复！',{icon: 7, title:'操作确认'}, function(index){
			//do something
			layer.close(index);
			var index = layer.load();
			$.post("{:url('del')}",{ids:ids},function(data){
				layer.close(index);
				dialog(data.msg,data.status);
				tableIns.reload()
			})
		});
	}

	function edit(id){
		var index = layer.load();
		$.get("{:url('getdetail')}",{id:id},function(res){
			layer.close(index)
			var data = res.data;
			var html = `
			<div class="layui-form" style="padding: 20px 0;">
				<input type="hidden" name="info[id]"  class="layui-input" value="`+id+`">
				<div class="layui-form-item">
					<label class="layui-form-label">分组名称：</label>
					<div class="layui-input-inline">
						<input type="text" name="info[name]"  class="layui-input" value="`+data.name+`">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">排序：</label>
					<div class="layui-input-inline" style="width:80px">
							<input type="text" name="info[sort]"  class="layui-input" value="`+data.sort+`">
					</div>
					<div class="layui-form-mid layui-word-aux">越大越靠前</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-input-block">
					<button class="layui-btn" lay-submit lay-filter="formsubmit">保 存</button>
					</div>
				</div>
			</div>
			`;
			layer.open({type:1,title:(id>0?'编辑分组':'添加分组'),area:'400px',content:html,resize:true,shadeClose:true});
		})
	}
  layui.form.on('submit(formsubmit)', function(obj){
	  var field = obj.field
	  var index = layer.load();
	  $.post("{:url('save')}",field,function(data){
		  layer.close(index);
		  dialog(data.msg,data.status);
		  if(data.status == 1){
			  setTimeout(function(){
				  layer.closeAll();
				  tableIns.reload()
			  },1000)
		  }
	  })
  })
	</script>
	{include file="public/copyright"/}
</body>
</html>